using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 属性Buff - 临时修改实体属���
/// </summary>
public class AttrBuff : BuffBase
{
    private Dictionary<AttributeType, float> m_AttributeModifiers;
    private Dictionary<AttributeType, float> m_OriginalAttributes;

    public AttrBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        m_AttributeModifiers = new Dictionary<AttributeType, float>();
        m_OriginalAttributes = new Dictionary<AttributeType, float>();
        
        if (buffData is AttrBuffData attrData)
        {
            // 复制属性修改器
            foreach (var modifier in attrData.AttributeModifiers)
            {
                m_AttributeModifiers[modifier.AttributeType] = modifier.ModifierValue;
            }
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始属性���
        SaveOriginalAttributes();
        
        // 应用属性修���
        ApplyAttributeModifiers();
        
        // 播放属性增强特���
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log($"{Target.name} 获得属性增强效���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 属性Buff期间不需要特殊处���
    }

    private void SaveOriginalAttributes()
    {
        // 保存移动速度
        if (Target.MoveComponent != null)
        {
            m_OriginalAttributes[AttributeType.MoveSpeed] = Target.MoveComponent.Speed;
        }
        
        // 保存攻击力（如果有AttrComponent���
        if (Target.AttrComponent != null)
        {
            // 这里需要根据AttrComponent的具体实现来保存属���
            // m_OriginalAttributes[AttributeType.AttackPower] = Target.AttrComponent.AttackPower;
        }
    }

    private void ApplyAttributeModifiers()
    {
        // 应用移动速度修改
        if (m_AttributeModifiers.ContainsKey(AttributeType.MoveSpeed) && Target.MoveComponent != null)
        {
            float originalSpeed = m_OriginalAttributes[AttributeType.MoveSpeed];
            float modifier = m_AttributeModifiers[AttributeType.MoveSpeed];
            float newSpeed = originalSpeed * (1.0f + modifier);
            Target.MoveComponent.SetSpeed(newSpeed);
        }
        
        // 应用攻击力修���
        if (m_AttributeModifiers.ContainsKey(AttributeType.AttackPower) && Target.AttrComponent != null)
        {
            // 这里需要根据AttrComponent的具体实现来修改属���
            // float originalAttack = m_OriginalAttributes[AttributeType.AttackPower];
            // float modifier = m_AttributeModifiers[AttributeType.AttackPower];
            // float newAttack = originalAttack * (1.0f + modifier);
            // Target.AttrComponent.SetAttackPower(newAttack);
        }
    }

    private void RestoreOriginalAttributes()
    {
        // 恢复移动速度
        if (m_OriginalAttributes.ContainsKey(AttributeType.MoveSpeed) && Target.MoveComponent != null)
        {
            Target.MoveComponent.SetSpeed(m_OriginalAttributes[AttributeType.MoveSpeed]);
        }
        
        // 恢复攻击���
        if (m_OriginalAttributes.ContainsKey(AttributeType.AttackPower) && Target.AttrComponent != null)
        {
            // 这里需要根据AttrComponent的具体实现来恢复属���
            // Target.AttrComponent.SetAttackPower(m_OriginalAttributes[AttributeType.AttackPower]);
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 属性增强效果结���");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始属���
        RestoreOriginalAttributes();
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("AttrBuff"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 属性Buff可以叠加，刷新持续时���
    }
}

/// <summary>
/// 属性类型枚���
/// </summary>
public enum AttributeType
{
    MoveSpeed,      // 移动速度
    AttackPower,    // 攻击���
    AttackSpeed,    // 攻击速度
    Health,         // 生命���
    Defense,        // 防御���
    CriticalChance, // 暴击���
    CriticalDamage, // 暴击伤害
    DodgeChance,    // 闪避���
    Accuracy        // 命中���
}

/// <summary>
/// 属性修改器
/// </summary>
[System.Serializable]
public class AttributeModifier
{
    public AttributeType AttributeType;
    public float ModifierValue; // 修改值（百分比形式，���.1表示增加10%���
}
